#include "detectorparsesystempacket.h"
#include "common/pfcid.h"
#include "packetformat.h"

#include "modulehplccmdid.h"
#include <QVariant>

namespace hplc
{
namespace qinfine
{

DetectorParseSystemPacket::DetectorParseSystemPacket(DetectorProtocolData &data, DetectorParam &param, QObject *parent)
    : QObject (parent)
    , m_param(&param)
{
    m_d = &data;
}

void DetectorParseSystemPacket::parseSet(quint8 pfc, char *data)
{
    switch (pfc)
    {
    case PFC_DES_COIL:
    {
        m_param->systemParam.coil = data[0];

        QVariant var;
        var.setValue(m_param->systemParam.coil);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetCoil, var);
    }
        break;
    case PFC_DES_TIME_INTEGRAL:
    {
        m_param->systemParam.integralParam.time = PTR2U16(data);

        QVariant var;
        var.setValue(m_param->systemParam.integralParam.time);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetTimeIntegral, var);
    }
        break;
    case PFC_DES_RANGE_INTEGRAL:
    {
        m_param->systemParam.integralParam.range =
                (hplc::IntegralRange)(data[0]);

        QVariant var;
        var.setValue(m_param->systemParam.integralParam.range);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetRangeIntegral, var);
    }
        break;
    case PFC_DES_AU_FACTOR:
    {
        quint32 temp = PTR2U32(data);
        m_param->systemParam.auFactor = *(float*)(&temp);

        QVariant var;
        var.setValue(m_param->systemParam.auFactor);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetAuFactor, var);
    }
        break;
    case PFC_DES_RASTER_STEP:
    {
        m_param->systemParam.rasterStep = PTR2U32(data);

        QVariant var;
        var.setValue(m_param->systemParam.rasterStep);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetRasterStep, var);
    }
        break;
    case PFC_DES_HOME_OFFSET:
    {
        m_param->systemParam.homeOffset = PTR2U32(data);

        QVariant var;
        var.setValue(m_param->systemParam.homeOffset);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetHomeOffset, var);
    }
        break;
    case PFC_DES_WAVE_OFFSET:
    {
        m_param->systemParam.waveOffset = PTR2U32(data);

        QVariant var;
        var.setValue(m_param->systemParam.waveOffset);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetWaveOffset, var);
    }
        break;
    case PFC_DES_WORKMODE:
    {
        m_param->systemParam.workMode = data[0];

        QVariant var;
        var.setValue(m_param->systemParam.workMode);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetWorkMode, var);
    }
        break;
    case PFC_DES_WAVECALIB_DATA:
    {
        hplc::DetectorWaveCalibData cell;
        quint32 temp = PTR2U32(data);
        cell.position = *(float*)(&temp);

        if (m_param->systemParam.waveCalibTable.size())
        {
            if (qFuzzyCompare(cell.position, m_param->systemParam.waveCalibTable.last().position))
                break;
        }

        temp = PTR2U32(data + 4);
        cell.wave = *(float*)(&temp);
        m_param->systemParam.waveCalibTable << cell;
    }
        break;
    case PFC_DES_WAVECALIB_DATA_CMD:
        if (data[0])
        {
//            qDebug() << "clear wavecalibtable";
            m_param->systemParam.waveCalibTable.clear();
        }
        else
        {
            // 保存

//            qDebug() << "wavecalibratetable = ";
//            foreach (auto var, m_param->systemParam.waveCalibTable)
//            {
//                qDebug() << var.position << var.wave;
//            }

            QVariant var;
            var.setValue(m_param->systemParam.waveCalibTable);
            emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetWaveCalibData, var);
        }
        break;
    case PFC_DES_LAMP_USED_TIME:
    {
        m_param->systemParam.lampUsedTime = PTR2U32(data);

        QVariant var;
        var.setValue(m_param->systemParam.lampUsedTime);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetLampUsedTime, var);
    }
        break;
    case PFC_DES_LAMP_ONOFF_TIMES:
    {
        m_param->systemParam.lampOnOffTimes = PTR2U16(data);

        QVariant var;
        var.setValue(m_param->systemParam.lampOnOffTimes);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetLampOnOffTimes, var);
    }
        break;
    case PFC_DES_LAMP_COUNT:
    {
        m_param->systemParam.lampCount = data[0];

        QVariant var;
        var.setValue(m_param->systemParam.lampCount);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetLampCount, var);
    }
        break;
    case PFC_DES_DAC_ZEROVOLTAGE:
    {
        quint32 temp = PTR2U32(data);
        m_param->systemParam.dacZeroVoltage = *(float*)(&temp);

        QVariant var;
        var.setValue(m_param->systemParam.dacZeroVoltage);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetDacZeroVoltage, var);
    }
        break;
    case PFC_DES_UPLOADOBJECT:
    {
        m_param->systemParam.uploadObject = (hplc::UploadObject)(data[0]);

        QVariant var;
        var.setValue(m_param->systemParam.uploadObject);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetUploadObject, var);
    }
        break;
    case PFC_DES_HOME_TIME_INTEGRAL:
    {
        m_param->systemParam.homeIntegralParam.time = PTR2U16(data);

        QVariant var;
        var.setValue(m_param->systemParam.homeIntegralParam.time);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetHomeTimeIntegral, var);
    }
        break;
    case PFC_DES_HOME_RANGE_INTEGRAL:
    {
        m_param->systemParam.homeIntegralParam.range =
                (hplc::IntegralRange)(data[0]);

        QVariant var;
        var.setValue(m_param->systemParam.homeIntegralParam.range);
        emit parseCompleted(hplc::qinfine::DetectorSystemCmdID_SetHomeRangeIntegral, var);
    }
        break;
    default:
        break;
    }
}

void DetectorParseSystemPacket::parseGet(quint8 pfc, char *data)
{
    switch (pfc)
    {
    case PFC_DES_COIL:
        break;
    case PFC_DES_TIME_INTEGRAL:
        break;
    case PFC_DES_RANGE_INTEGRAL:
        break;
    case PFC_DES_AU_FACTOR:
        break;
    case PFC_DES_RASTER_STEP:
        break;
    case PFC_DES_HOME_OFFSET:
        break;
    case PFC_DES_WAVE_OFFSET:
        break;
    case PFC_DES_WORKMODE:
        break;
    case PFC_DES_WAVECALIB_DATA:
        break;
    case PFC_DES_WAVECALIB_DATA_CMD:
        break;
    case PFC_DES_LAMP_USED_TIME:
        break;
    case PFC_DES_LAMP_ONOFF_TIMES:
        break;
    case PFC_DES_LAMP_COUNT:
        break;
    case PFC_DES_DAC_ZEROVOLTAGE:
        break;
    case PFC_DES_UPLOADOBJECT:
        break;
    case PFC_DES_HOME_TIME_INTEGRAL:
        break;
    case PFC_DES_HOME_RANGE_INTEGRAL:
        break;
    default:
        break;
    }
}

} // qinfine
} // hplc
